package com.example.demo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.nio.file.Paths;
import java.util.Collections;

/**
 * @ClassName CodeGenerator
 * @Description 代码生成器
 * @Author MingChang.Wei
 * @Date 2024/9/23
 **/
public class CodeGenerator {
    public static void main(String[] args) {
        // 示例：生成PDF模块代码（如果需要）
        // generate("pdf", "your_table_name");
        
        // 示例：生成其他模块代码
        // generate("user", "sys_user");
        // generate("order", "t_order");
        
        System.out.println("请根据需要修改模块名和表名后运行代码生成器");
    }

    public static void generate(String moduleName, String tableName) {
        FastAutoGenerator.create(new DataSourceConfig.Builder("jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&useSSL=false", "root", "123456"))
                .globalConfig(builder -> builder
                        .author("wmc")
                        .disableOpenDir()
                        .outputDir(Paths.get(System.getProperty("user.dir")) + "/src/main/java")
                        .commentDate("yyyy-MM-dd")
                        .dateType(DateType.ONLY_DATE)
                )
                .packageConfig(builder -> builder
                        .parent("com.example.demo")
                        .moduleName(moduleName)
                        .entity("entity")
                        .service("service")
                        .serviceImpl("service.impl")
                        .mapper("mapper")
                        .pathInfo(Collections.singletonMap(OutputFile.xml, Paths.get(System.getProperty("user.dir")) + "/src/main/resources/mapper"))
                        .controller("controller")
                )
                .strategyConfig(builder -> builder
                        .addInclude(tableName)
                        .entityBuilder()    // entity配置
                        .enableFileOverride()
                        .enableLombok()
                        .enableChainModel()
                        .naming(NamingStrategy.underline_to_camel)
                        .columnNaming(NamingStrategy.underline_to_camel)
                        .idType(IdType.AUTO)
                        .formatFileName("%s")
                        .mapperBuilder()    // mapper配置
                        .enableBaseResultMap()
                        .enableBaseColumnList()
                        .formatMapperFileName("%sMapper")
                        .formatXmlFileName("%sMapper")
                        .serviceBuilder()   // service配置
                        .formatServiceFileName("%sService")
                        .formatServiceImplFileName("%sServiceImpl")
                        .controllerBuilder()    // controller配置
                        .enableRestStyle()
                        .formatFileName("%sController")
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
